package com.jiji.miaosha.mapper.provide;

import com.jiji.miaosha.entity.User;
import org.apache.ibatis.jdbc.SQL;

import java.util.Map;

public class UserMapperProvider extends BaseProvider {

    public String upSert(final Map<String, Object> map) {
        User user = (User) map.get("user");
        StringBuffer sb = new StringBuffer(INSERT_USER).append(UPDATE);
        if (user.getName() != null) {
            sb.append("name=#{user.name},");
        }
        if (user.getGender() != null) {
            sb.append("gender=#{user.gender},");
        }
        if (user.getAge() != null) {
            sb.append("age=#{user.age},");
        }
        if (user.getTelephone() != null && !"".equals(user.getTelephone().trim())) {
            sb.append("telephone=#{user.telephone},");
        }
        if (user.getRegisterMode() != null) {
            sb.append("register_mode=#{user.registerMode},");
        }
        if (user.getThirdPartId() != null) {
            sb.append("third_part_id=#{user.thirdPartId}");
        }
        return sb.toString();
    }

    public String insertSelective(final Map<String, Object> map) {
        User user = (User) map.get("user");
        SQL sql = new SQL() {
            {
                INSERT_INTO(USER.trim());
                if (user.getName() != null) {
                    VALUES("name", "'" + user.getName() + "'");
                }
                if (user.getGender() != null) {
                    VALUES("gender", String.valueOf(user.getGender()));
                }
                if (user.getAge() != null) {
                    VALUES("age", String.valueOf(user.getAge()));
                }
                if (user.getTelephone() != null && !"".equals(user.getTelephone().trim())) {
                    VALUES("telephone", user.getTelephone());
                }
                if (user.getRegisterMode() != null) {
                    VALUES("register_mode", "'" + user.getRegisterMode() + "'");
                }
                if (user.getThirdPartId() != null) {
                    VALUES("third_part_id", "'" + user.getThirdPartId() + "'");
                }
            }
        };
        return sql.toString();
    }
}
